package com.fr.data.core;

import com.fr.base.ClassComparator;
import com.fr.base.FRContext;
import com.fr.base.XMLable;
import com.fr.base.core.FRCoreContext;
import com.fr.base.file.DatasourceManager;
import com.fr.base.xml.XMLObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary;
import com.fr.data.TableData;
import com.fr.data.condition.CommonCondition;
import com.fr.data.condition.Condition;
import com.fr.data.condition.FormulaCondition;
import com.fr.data.condition.ListCondition;
import com.fr.data.condition.ObjectCondition;
import com.fr.data.core.define.DataSource;
import com.fr.data.core.define.FileDataSource;
import com.fr.data.core.define.URLDataSource;
import com.fr.data.impl.Connection;
import com.fr.data.impl.CustomDictionary;
import com.fr.data.impl.DBTableData;
import com.fr.data.impl.DatabaseDictionary;
import com.fr.data.impl.DirectoryConnection;
import com.fr.data.impl.EmbeddedTableData;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.data.impl.JNDIDatabaseConnection;
import com.fr.data.impl.NameDatabaseConnection;
import com.fr.data.impl.NameDictionary62;
import com.fr.data.impl.NameTableData;
import com.fr.data.impl.TableDataDictionary;
import com.fr.data.util.function.DataFunction;
import com.fr.data.util.function.NoneFunction;
import com.fr.report.web.ui.WidgetConfig;
import com.fr.report.web.ui.reg.NoneReg;
import com.fr.report.web.ui.reg.RegExp;
import com.fr.util.Utils;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataXMLUtils
{
  public static void writeXMLDataSource(XMLPrintWriter paramXMLPrintWriter, DataSource paramDataSource)
  {
    if (paramDataSource == null)
      return;
    paramXMLPrintWriter.startTAG("DataSource").attr("class", paramDataSource.getClass().getName());
    paramDataSource.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static DataSource readXMLDataSource(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      str = str.trim();
      if (str.endsWith(".FileDataSource"))
        localObject = new FileDataSource();
      else if (str.endsWith(".URLDataSource"))
        localObject = new URLDataSource();
      else
        try
        {
          localObject = (DataSource)FRCoreContext.classForName(str).newInstance();
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      if (localObject instanceof XMLable)
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
    }
    return ((DataSource)localObject);
  }

  public static void writeXMLConnection(XMLPrintWriter paramXMLPrintWriter, Connection paramConnection)
  {
    writeXMLConnection(paramXMLPrintWriter, paramConnection, null);
  }

  public static void writeXMLConnection(XMLPrintWriter paramXMLPrintWriter, Connection paramConnection, String paramString)
  {
    if (paramConnection == null)
      return;
    paramXMLPrintWriter.startTAG("Connection");
    if (paramString != null)
      paramXMLPrintWriter.attr("name", paramString);
    paramXMLPrintWriter.attr("class", paramConnection.getClass().getName());
    paramConnection.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static void writeXMLTableData(XMLPrintWriter paramXMLPrintWriter, TableData paramTableData)
  {
    writeXMLTableData(paramXMLPrintWriter, paramTableData, null);
  }

  public static void writeXMLTableData(XMLPrintWriter paramXMLPrintWriter, TableData paramTableData, String paramString)
  {
    if (paramTableData == null)
      return;
    paramXMLPrintWriter.startTAG("TableData");
    if (paramString != null)
      paramXMLPrintWriter.attr("name", paramString);
    paramXMLPrintWriter.attr("class", paramTableData.getClass().getName());
    paramTableData.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static void writeXMLWidgetConfig(XMLPrintWriter paramXMLPrintWriter, WidgetConfig paramWidgetConfig, String paramString)
  {
    if (paramWidgetConfig == null)
      return;
    paramXMLPrintWriter.startTAG("WidgetConfig");
    if (paramString != null)
      paramXMLPrintWriter.attr("name", paramString);
    paramXMLPrintWriter.attr("class", paramWidgetConfig.getClass().getName());
    paramWidgetConfig.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static void writeXMLDictionary(XMLPrintWriter paramXMLPrintWriter, Dictionary paramDictionary)
  {
    writeXMLDictionary(paramXMLPrintWriter, paramDictionary, null);
  }

  public static void writeXMLDictionary(XMLPrintWriter paramXMLPrintWriter, Dictionary paramDictionary, String paramString)
  {
    if (paramDictionary == null)
      return;
    paramXMLPrintWriter.startTAG("Dictionary");
    if (paramString != null)
      paramXMLPrintWriter.attr("name", paramString);
    paramXMLPrintWriter.attr("class", paramDictionary.getClass().getName());
    paramDictionary.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static DataFunction readXMLDataFunction(String paramString)
  {
    paramString = Utils.replaceAllString(paramString, "com.fr.data.function", "com.fr.data.impl.function");
    paramString = Utils.replaceAllString(paramString, "com.fr.data.impl.function", "com.fr.data.util.function");
    try
    {
      return ((DataFunction)FRCoreContext.classForName(paramString).newInstance());
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return new NoneFunction();
  }

  public static Connection readXMLConnectionMap(XMLableReader paramXMLableReader)
  {
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    1 local1 = new XMLObject(null, localDatasourceManager)
    {
      private final DatasourceManager val$datasourceManager;

      public void readXML(XMLableReader paramXMLableReader)
      {
        if (paramXMLableReader.isAttr())
          this.val$datasourceManager.clearAllConnection();
        if ((paramXMLableReader.isChildNode()) && ("Connection".equals(paramXMLableReader.getTagName())))
        {
          Object localObject = null;
          String str = null;
          if ((str = paramXMLableReader.getAttr("name")) != null)
            localObject = str;
          if (localObject == null)
            return;
          this.obj = DataXMLUtils.readXMLConnection(paramXMLableReader);
          this.val$datasourceManager.putConnection(localObject, (Connection)this.obj);
        }
      }
    };
    paramXMLableReader.readXMLObject(local1);
    return ((Connection)local1.getObject());
  }

  public static Connection readXMLConnection(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      if (str.indexOf(".JDBCDatabase") > 0)
        localObject = new JDBCDatabaseConnection();
      else if (str.indexOf(".JNDIDatabase") > 0)
        localObject = new JNDIDatabaseConnection();
      else if (str.endsWith(".NameDatabase"))
        localObject = new NameDatabaseConnection();
      else if ((str.indexOf(".DirectoryConnection") > 0) || (str.endsWith(".TextDatasource")))
        localObject = new DirectoryConnection();
      else
        try
        {
          localObject = (Connection)FRCoreContext.classForName(str).newInstance();
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      if (localObject != null)
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
    }
    return ((Connection)localObject);
  }

  public static TableData readXMLTableData(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
      if (str.endsWith(".TableData$1"))
      {
        localObject = TableData.EMPTY_TABLEDATA;
      }
      else
      {
        if ((str.endsWith(".DBTableData")) || (str.endsWith(".JDBCTableData")) || (str.equals("JDBCTableData")))
          localObject = new DBTableData();
        else if (str.endsWith(".EmbeddedTableData"))
          localObject = new EmbeddedTableData();
        else if (str.endsWith(".NameTableData"))
          localObject = new NameTableData();
        else
          try
          {
            localObject = (TableData)FRCoreContext.classForName(str).newInstance();
          }
          catch (Exception localException)
          {
            FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
            return null;
          }
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
      }
    return ((TableData)localObject);
  }

  public static WidgetConfig readXMLWidgetConfig(XMLableReader paramXMLableReader)
  {
    String str;
    WidgetConfig localWidgetConfig = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      try
      {
        localWidgetConfig = (WidgetConfig)FRCoreContext.classForName(str).newInstance();
      }
      catch (InstantiationException localInstantiationException)
      {
        FRContext.getLogger().log(Level.WARNING, localInstantiationException.getMessage(), localInstantiationException);
      }
      catch (IllegalAccessException localIllegalAccessException)
      {
        FRContext.getLogger().log(Level.WARNING, localIllegalAccessException.getMessage(), localIllegalAccessException);
      }
      catch (ClassNotFoundException localClassNotFoundException)
      {
        FRContext.getLogger().log(Level.WARNING, localClassNotFoundException.getMessage(), localClassNotFoundException);
      }
      paramXMLableReader.readXMLObject(localWidgetConfig);
    }
    return localWidgetConfig;
  }

  public static RegExp readXMLReg(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
      try
      {
        localObject = (RegExp)FRCoreContext.classForName(str).newInstance();
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        localObject = new NoneReg();
      }
    paramXMLableReader.readXMLObject((XMLReadable)localObject);
    return ((RegExp)localObject);
  }

  public static Dictionary readXMLDictionary(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      if ((str.endsWith(".TableDataDictionary")) || (str.endsWith(".TableDataDict")) || (str.endsWith(".TableDataMap")))
      {
        localObject = new TableDataDictionary();
      }
      else if ((str.endsWith(".DatabaseDictionary")) || (str.endsWith(".DBDict")) || (str.endsWith(".DBMap")) || (str.endsWith(".JDBCMap")) || (str.endsWith(".SimpleDBMap")))
      {
        localObject = new DatabaseDictionary();
      }
      else if ((str.endsWith(".CustomDictionary")) || (str.endsWith(".CustomDict")) || (str.endsWith(".CustomMap")) || (str.endsWith(".ListMap")))
      {
        localObject = new CustomDictionary();
      }
      else
      {
        if (str.endsWith("NameDictionary"))
        {
          NameDictionary62 localNameDictionary62 = new NameDictionary62();
          paramXMLableReader.readXMLObject(localNameDictionary62);
          return localNameDictionary62.createDict();
        }
        if ((str.endsWith("TreeRendererMVList")) || (str.endsWith("RendererMVList")))
          localObject = new CustomDictionary();
        else
          try
          {
            localObject = (Dictionary)FRCoreContext.classForName(str).newInstance();
          }
          catch (Exception localException)
          {
            FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
            localObject = new CustomDictionary();
          }
      }
      paramXMLableReader.readXMLObject((XMLReadable)localObject);
    }
    return ((Dictionary)localObject);
  }

  public static Condition readCondition(XMLableReader paramXMLableReader)
  {
    String str;
    if ((str = paramXMLableReader.getAttr("class")) != null)
      try
      {
        Condition localCondition = (Condition)FRCoreContext.classForName(str).newInstance();
        paramXMLableReader.readXMLObject(localCondition);
        return localCondition;
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    return null;
  }

  public static Comparator readXMLComparator(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      if (str.endsWith(".ClassComparator"))
        localObject = new ClassComparator();
      else
        try
        {
          localObject = (Comparator)FRCoreContext.classForName(str).newInstance();
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      if (localObject instanceof XMLable)
        paramXMLableReader.readXMLObject((XMLable)localObject);
    }
    return ((Comparator)localObject);
  }

  public static void writeXMLComparator(XMLPrintWriter paramXMLPrintWriter, Comparator paramComparator)
  {
    if (paramComparator == null)
      return;
    paramXMLPrintWriter.startTAG("Comparator").attr("class", paramComparator.getClass().getName());
    if (paramComparator instanceof XMLable)
      ((XMLable)paramComparator).writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public static Condition readXMLCondition(XMLableReader paramXMLableReader)
  {
    String str;
    Object localObject = null;
    if ((str = paramXMLableReader.getAttr("class")) != null)
    {
      if (str.endsWith(".CommonCondition"))
        localObject = new CommonCondition();
      else if (str.endsWith(".FormulaCondition"))
        localObject = new FormulaCondition();
      else if (str.endsWith(".ListCondition"))
        localObject = new ListCondition();
      else if (str.endsWith(".ObjectCondition"))
        localObject = new ObjectCondition();
      else
        try
        {
          Class localClass = FRCoreContext.classForName(str);
          localObject = (Condition)localClass.newInstance();
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
          localObject = new ListCondition();
        }
      if (localObject instanceof XMLable)
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
    }
    return ((Condition)localObject);
  }

  public static void writeXMLCondition(XMLPrintWriter paramXMLPrintWriter, Condition paramCondition)
  {
    if (paramCondition == null)
      return;
    paramXMLPrintWriter.startTAG("Condition").attr("class", paramCondition.getClass().getName());
    if (paramCondition instanceof XMLable)
      paramCondition.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }
}